<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" class="ele-form-search" label-width="77px" @keyup.enter.native="reload"
               @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="工单名称:">
              <el-input v-model="where.name" clearable placeholder="请输入工单名称" />
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button v-if="permission.includes('sys:product:index')" class="ele-btn-icon" icon="el-icon-search"
                         type="primary" @click="reload">
                查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :columns="columns" :datasource="url" :selection.sync="selection" :where="where"
                     height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-dropdown @command="handleCommand">
            <el-button icon="el-icon-plus" size="small" type="primary">
              添加
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-for="item in typeList" :key="item" :command="item">{{ item }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
        <!--工单编号-->
        <template slot="wo_number" slot-scope="{row}">
          <span style="cursor: pointer;color: rgb(37, 99, 235);" @click="$router.push('/workTickets/workTickets/info?id=' + row.id)">
            {{ row.wo_number }}
          </span>
        </template>

        <!--创建方式-->
        <template slot="createType">
          <span>客服创建</span>
        </template>
        <!--派单人-->
        <template slot="dispatch" slot-scope="{row}">
          <span >{{row.logs.length>=2?row.logs[1].creator:''}}</span>
        </template>
        <!--产品-->
        <template slot="products" slot-scope="{row}">
          {{ formProducts(row) }}
        </template>
        <!--地址-->
        <template slot="address" slot-scope="{row}">
          <span>{{ row.province_name }} {{ row.city_name }} {{ row.county_name }} {{ row.address }}</span>
        </template>
        <!--负责人-->
        <template slot="master" slot-scope="{row}">
          <span>{{ formMaster(row) }}</span>
        </template>
        <!--协调人-->
        <template slot="synergistic_people" slot-scope="{row}">
          <span>{{ formSynergistic_people(row) }}</span>
        </template>
      </ele-pro-table>
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from "vuex";

export default {
  name: 'SystemLevel',
  computed: {
    ...mapGetters(["permission"])
  },
  data() {
    return {
      typeList: [
        "安装工单",
        "设备维修",
        "维保工单",
        "巡检工单",
      ],
      // 表格数据接口
      url: '/wos?status=complete',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          slot: 'wo_number',
          label: '工单编号',
          width: 150,
          align: 'center',
          showOverflowTooltip: true,
        },
        {
          prop: 'type',
          label: '工单类型',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center',
        },
        {
          prop: 'customer.name',
          label: '客户',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center'
        },
        {
          prop: 'contact_name',
          label: '联系人',
          align: 'center',
          showOverflowTooltip: true,
          width: 100
        },

        {
          prop: 'contact_phone',
          label: '电话',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          slot: 'address',
          label: '客户地址',
          showOverflowTooltip: true,
          minWidth: 250,
          align: 'center'
        },
        {
          slot: "products",
          label: '产品',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center'
        },
        {
          prop: 'plan_at',
          label: '计划时间',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center'
        },
        {
          slot: 'master',
          label: '负责人',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          slot: 'synergistic_people',
          label: '协同人',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'status',
          label: '工单状态',
          minWidth: 160,
          align: 'center',
        },
        {
          prop: 'create_user.nickname',
          label: '创建人',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
        },
        {
          prop: 'create_time_format',
          label: '创建时间',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
        },
        {
          slot: 'dispatch',
          label: '派单人',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
        },
        {
          slot: 'createType',
          label: '创建方式',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',

        },
        // {
        //   columnKey: 'action',
        //   label: '操作',
        //   width: 150,
        //   align: 'center',
        //   resizable: false,
        //   slot: 'action',
        //   fixed: "right"
        // }
      ],
      //显示新增联系人
      showCustomerUserEdit: false,
      //显示编辑选项
      isEditType: false,
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      //当前编辑的类型
      type: null,
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 二维码是否显示编辑弹窗
      codeEdit: false,
      // 当前编辑数据
      currentCode: null,
      // 是否显示导入弹窗
      showImport: false,
      //客户id
      customer_id: null,
      //客户名字
      customer_name: null,
    };
  },
  methods: {
    //数据处理
    formProducts(row) {
      return (row.products.map(item => {
        return item.product.name
      })).join(' ')
    },
    formMaster(row) {
      if (row.users.filter((item) => {
        return item.is_master === 1
      })) {
        if (((row.users.filter((item) => {
          return item.is_master === 1
        }))[0])) {
          return (((row.users.filter((item) => {
            return item.is_master === 1
          }))[0]).user).realname
        }
      }
    },
    formSynergistic_people(row) {
      return ((row.users.filter((item) => {
        return item.is_master === 0
      })).map(item => {
        return item.user.realname
      })).join(' ')
    },
    handleCommand(command) {
      this.$router.push({ path: '/workTickets/workTickets/addWorkTicket', query: { type: command } })
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.type = ''
      this.isEditType = false;
      this.where = {};
      this.reload();
    }
  }
}
</script>

<style>

</style>
